<?php

function shas($n) { return isset($_SESSION[$n]); }
function sget($n) { return $_SESSION[$n]; }
function sset($n,$v) { $_SESSION[$n] = $v; }

function phas($n) { return isset($_POST[$n]); }
function pget($n) { return $_POST[$n]; }

session_start();

if(phas('logout')) {
  session_destroy();
  session_start();
}

if(phas('username')) {
  sset('username', pget('username'));
  sset('time', time());
}

if(shas('username')) {
?>
  <p>Logged in as: <?=sget('username')?></p>
  <p>Last login: <?=sget('time')?></p>
  <form method="POST">
    <input type="hidden" name="logout" />
    <button>log out</button>
  </form>
<?php
}
else {
?>
  <form method="POST"> 
    <input type="text" name="username">
    <button>log in</button>
  </form>
<?php
}

